package com.android.tripspins.app.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;

import com.android.tripspins.app.R;
import com.android.tripspins.app.bo.Attachment;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.ProfilePictureView;

import java.util.ArrayList;

/**
 * Created by João.Rutkoski on 15/10/13.
 */
public class AttachmentListAdapter extends BaseAdapter {

    private final Activity activity;
    private ArrayList<Attachment> attachments;
    private static LayoutInflater inflater = null;
    private UiLifecycleHelper uiHelper;

    public AttachmentListAdapter(Activity a, ArrayList<Attachment> attachments, UiLifecycleHelper uiHelper)
    {
        this.attachments = attachments;
        activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.uiHelper = uiHelper;
    }
    public AttachmentListAdapter(Activity a, ArrayList<Attachment> attachments)
    {
        this.attachments = attachments;
        activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return attachments.size();
    }

    @Override
    public Object getItem(int i) {
        return attachments.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        final int index = i;
        if(view == null)
            v = inflater.inflate(R.layout.new_pin_attachment_row,null);
        v.setClickable(true);
        v.setFocusable(true);

        ImageButton imgThumbnail = (ImageButton)v.findViewById(R.id.new_pin_img_att);

        String photoUri;
        if(attachments.get(i).getThumbnailPath()==null )
        {

            ProfilePictureView fbContactPiture = (ProfilePictureView)v.findViewById(R.id.new_pin_att_fb_picture);
            fbContactPiture.setProfileId(attachments.get(i).getContact().getContactIdOnSource());
            fbContactPiture.setVisibility(View.VISIBLE);
            imgThumbnail.setVisibility(View.INVISIBLE);
        }
        else{
            photoUri = attachments.get(i).getThumbnailPath();
            try{
                imgThumbnail.setImageURI(Uri.parse(photoUri));
            }catch(OutOfMemoryError error)
            {
                try{

                    System.gc();
                    imgThumbnail.setImageURI(Uri.parse(photoUri));
                }
                catch(OutOfMemoryError error2)
                {

                }
            }
        }

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DeleteItemDialog dialog = new DeleteItemDialog();
                dialog.show(activity.getFragmentManager(), String.valueOf(index));

            }
        });

        return v;
    }
    private void deleteAttachment(int index, int gridIndex) {
        if(index == 0)
        {
            attachments.remove(gridIndex);
            notifyDataSetChanged();
        }
    }

    private class DeleteItemDialog extends DialogFragment {


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(R.string.dialog_delete_attachment);
            final Bundle savedInstance = savedInstanceState;
            final int gridIndex = Integer.parseInt(getTag());
            builder.setItems(R.array.dialog_delete_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int index) {
                    deleteAttachment(index, gridIndex);
                }
            });
            return builder.create();
        }
    }


}
